package players;

import java.util.ArrayList;

import model.*;

public class MobilityPlayer implements Player
{
	public boolean move(OthelloBoard board, byte color) 
	{
		// get all possible transitions
		ArrayList<Position> moves = board.getTransitions(color);
		if (moves.size() == 0)
		{
			return false;
		}
		
		// find the best one
		double bestScore = -1000.0f;
		Position bestMove = null;
		for(Position move : moves)
		{
			// transition nr k
			OthelloBoard newState = board.getClone();
			newState.makeTransition(move, color);
			
			// evaluate new state
			double score = evaluate(newState, color);
			
			if(score > bestScore)
			{
				bestScore = score;
				bestMove = move;
			}
			
		}
		
		// transition to 'best' new state
		board.makeTransition(bestMove, color);
		return true;
	}
	
	public double evaluate(OthelloBoard board, byte color)
	{
		// maximize nr of possible moves of player
		// minimize nr of possible moves of opponent
		// so: eval(state) = 10*(nCornersPl - nCornersOp) + (nMovesPl - nMovesOp)/(nMovesPl + nMovesOp)
		
		// count corner positions
		int cPlayer = 0, cOpponent = 0;
		
		if(board.at(0, 0) == Field.opponent(color)) cOpponent++; 
		if(board.at(0, 0) == color)         cPlayer++;
		if(board.at(0, board.size - 1) == Field.opponent(color)) cOpponent++; 
		if(board.at(0, board.size - 1) == color)         cPlayer++;
		if(board.at(board.size - 1, 0) == Field.opponent(color)) cOpponent++; 
		if(board.at(board.size - 1, 0) == color)   cPlayer++;
		if(board.at(board.size - 1, board.size - 1) == Field.opponent(color)) cOpponent++; 
		if(board.at(board.size - 1, board.size - 1) == color)   cPlayer++;

		// count mobility
		int mPlayer = board.getTransitions(color).size();
		int mOpponent = board.getTransitions(Field.opponent(color)).size();
		
		if(mOpponent == 0 && mPlayer == 0) 
			return 1.0f;
		return 10.0f*(cPlayer - cOpponent) + (mPlayer - mOpponent) / (mPlayer + mOpponent + 0.0f);
	}

	public String toString()
	{
		return "Mobility";
	}
	
	@Override
	public void setExploration(boolean aan) {
		// TODO Auto-generated method stub
		
	}
}
